// ================================================================================
// 		File: PatyEduSubject.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（--）
// 		Date: 2015-12-27 22:18
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using ZHDJ.Data.Models.Box;

namespace ZHDJ.Data.Models.Edu
{
    /// <summary>
    /// 题目
    /// </summary>   
    public class PartyEduSubject : EntityBaseOperation
    {
        #region 属性

        /// <summary>
        /// 题目类型 选择题  填空题  判断题
        /// </summary>
        [StringLength(128)]
        public string SubjectType { get; set; }

        /// <summary>
        /// 所属专题
        /// </summary>
        [StringLength(128)]
        public string SpecialID { get; set; }

        /// <summary>
        /// 题目内容
        /// </summary>
        [Required, StringLength(256)]
        public string Subjectcontent { get; set; }

        /// <summary>
        /// 答案  （填空题用）
        /// </summary>
        [StringLength(256)]
        public string Answer { get; set; }

        /// <summary>
        /// 状态
        /// </summary>
        [Required]
        public bool Enabled { get; set; }

        /// <summary>
        /// 是否正确 （判断题用）
        /// </summary>
        public bool IsRight { get; set; }

        public virtual ICollection<PartyEduSubjectOptions> PatyEduSubjectOptions { get; set; }
        public virtual PartyEduSpecial PatyEduSpecial { get; set; }


        #endregion
        #region 数据查询

        /// <summary>
        /// 获取测试题
        /// </summary>
        /// <param name="top">几条</param>
        /// <returns></returns>
        public static DataSet GetPartyEduSubjectTest(int top, string specialFlag)
        {
            string sql = @"CREATE TEMPORARY TABLE tempT select ID,Subjectcontent,SpecialID,SubjectType from tblPartyEduSubject d
                where d.Enabled=1  and d.SpecialID =(select id from tblPartyEduSpecial Es where Es.SpecialFlag=?SpecialFlag LIMIT 1 )
ORDER BY  RAND() LIMIT " + top + @";
select * from tempT;
select ID,OptionsContent,IsAnswer,SubjectID from tblPartyEduSubjectOptions s
where EXISTS (select ID from tempT where tempT.ID=s.SubjectID ) order by Orderno;

drop table tempT;";
            MySqlParameter[] para = new MySqlParameter[] {
                new MySqlParameter() {
                DbType =DbType.String, ParameterName= "SpecialFlag",Value= specialFlag }
            };
            return MySqlHelper.ExecuteDataSet(sql, para);
        }

        /// <summary>
        /// 获取测试题
        /// </summary>
        /// <returns></returns>
        public static DataSet GetPartyEduSubjectTest(string[] subjectIDs)
        {
            if (subjectIDs == null)
            {
                subjectIDs[0] = "";
            }
            var paraStr = "";
            int index = 0;
            MySqlParameter[] para = new MySqlParameter[subjectIDs.Count()];
            foreach (var item in subjectIDs)
            {
                paraStr += "?SubjectID" + index + ",";
                para[index] = new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "SubjectID" + index,
                    Value = item
                };

                index += 1;
            }
            paraStr = paraStr.Trim(',');
            string sql = @"CREATE TEMPORARY TABLE tempT select ID,Subjectcontent,SpecialID,SubjectType from tblPartyEduSubject d
                where d.Enabled=1 and ID in (" + paraStr + @");
select * from tempT;
select ID,OptionsContent,IsAnswer,SubjectID from tblPartyEduSubjectOptions s
where EXISTS (select ID from tempT where tempT.ID=s.SubjectID ) order by Orderno;

drop table tempT;";

            return MySqlHelper.ExecuteDataSet(sql, para);
        }
        #endregion

        #region  导入
        public static DataSet GetGridData(string specialid, string title)
        {
            string sql = @"select a.ID,a.Subjectcontent,b.SpecialName,date_format(a.CreateTime,'%Y-%m-%d') as CreateTime ,
IF (a.Enabled='1','是','否') as EnabledState
                                from tblPartyEduSubject a 
                                left join tblpartyeduspecial b on a.SpecialID=b.ID 
                                where 1=1";
            return MySqlHelper.ExecuteDataSet(sql);
        }
        #endregion
    }


}
